package timing.ukulele.third.controller.weixin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.third.data.weixin.WxUserDTO;
import timing.ukulele.data.third.view.weixin.WxUserVO;
import timing.ukulele.third.service.weixin.WxUserService;

/**
 * 微信用户
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/wxUser")
public class WxUserController {

	private final WxUserService wxUserService;

    /**
     * /wxUser/page
     * @param wxUserDTO
     * @return
     */
	@Operation(summary = "分页查询")
	@PostMapping("/page")
	public ResponseData<IPage<WxUserVO>> pageList(@RequestBody WxUserDTO wxUserDTO) {
        return ResponseData.success(wxUserService.pageList(wxUserDTO));
	}

    /**
     * /wxUser/getDetails
     * @param id
     * @return
     */
	@Operation(summary = "通过ID查询微信用户")
	@GetMapping("/getDetails")
	public ResponseData<WxUserVO> getDetails(@RequestParam("id") Long id) {
        return ResponseData.success(wxUserService.getDetails(id));
	}

    /**
     * /wxUser/syncWxUser
     * @param appId
     * @return
     */
	@Operation(summary = "同步微信用户")
	@GetMapping("/syncWxUser")
	public ResponseData<Void> syncWxUser(@RequestParam("appId") String appId) {
        try {
            wxUserService.synchroWxUser(appId);
            return ResponseData.success();
        } catch (WxErrorException e) {
            return ResponseData.error("同步微信用户失败" + e.getMessage());
        }
	}

	/**
	 * /wxUser/updateRemark
	 * @param wxUserDTO
	 * @return
	 */
	@Operation(summary = "修改微信用户备注")
	@PostMapping("/updateRemark")
	public ResponseData<Boolean> updateRemark(@RequestBody WxUserDTO wxUserDTO) {
        try {
            return ResponseData.success(wxUserService.updateRemark(wxUserDTO));
        } catch (WxErrorException e) {
            return ResponseData.error("修改微信用户备注失败" + e.getMessage());
        }
    }

	/**
	 * /wxUser/setTag
	 * @param wxUserDTO
	 * @return
	 */
	@Operation(summary = "打标签")
	@PostMapping("/setTag")
	public ResponseData<Void> setTag(@RequestBody WxUserDTO wxUserDTO) {
        try {
            wxUserService.setTag(wxUserDTO);
            return ResponseData.success();
        } catch (WxErrorException e) {
            return ResponseData.error("打标签失败" + e.getMessage());
        }
	}

}
